package com.example.library

import android.content.pm.PackageManager
import androidx.fragment.app.Fragment

//在InvisibleF ragment 中，我们并没有重写onCreateView()方法来加载某个布
//局，因此它自然就是一个不可见的Fragment ，待会只需要将它添加到Activity 中即可

typealias PermissionCallback=(Boolean,List<String>)->Unit
class InvisibleFragment :Fragment() {
    private var callBack:PermissionCallback?=null
    fun requestNow(cb:PermissionCallback,vararg permission:String){
        callBack =cb
        requestPermissions(permission,1)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {

        if(requestCode ==1){
            val deniedList=ArrayList<String>()
            for((index,result) in grantResults.withIndex()){
                if(result!=PackageManager.PERMISSION_GRANTED){
                    deniedList.add(permissions[index])
                }
            }
            val allGranted = deniedList.isEmpty()
            callBack?.let {
                it(allGranted,deniedList)
            }
        }
    }
}